/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.prep;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;


/**
 *
 * @author baga
 */
public class MagXML {
    private File filePath;

    public MagXML(File path) {
        filePath = path;
    }

    public MagXML() {
        this(new File(""));
    }

    private void contXMLFile(String filename, HashMap prop) {
        Element root = new Element("ta_pa");
        Document doc = new Document(root);

        Set propSet = prop.keySet();
        Iterator iterProp = propSet.iterator();
        while (iterProp.hasNext()) {
            String propName = (String) iterProp.next();
            String propVal = (String) prop.get(propName);
            Element elm = new Element(propName);
            elm.setText(propVal);
            root.addContent(elm);
        }

        XMLOutputter outputer = new XMLOutputter();
        try {
            FileOutputStream output = new FileOutputStream(filename);
            outputer.output(doc, output);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void explores() {
        try {
            BufferedReader in = new BufferedReader(new FileReader(filePath));
            String s;
            int i = 0;
            StringBuilder sb = new StringBuilder();
            
            while ((s = in.readLine()) != null) {
                if (s.charAt(0) == '*') {
                    HashMap hm = new HashMap();
                    if (sb.length() > 0) {
                        String[] strTemp = s.split(" ");
                        int counts = 0;
                        String fileName = null;
                        for (int j=0; j<strTemp.length; j++) {
                            if (!strTemp[j].isEmpty()) {
                                switch (counts) {
                                    case 1:
                                        fileName = strTemp[j];
                                        hm.put("text", strTemp[j]);
                                        break;
                                    case 2 :
                                        hm.put("date", strTemp[j]);
                                        break;
                                    case 4 :
                                        hm.put("page", strTemp[j]);
                                        break;
                                }
                                counts++;
                            }
                        }
                        hm.put("news", sb.toString());
                        contXMLFile(fileName + ".xml", hm);
                        sb.delete(0, sb.length());
                    }
                } else {
                    sb.append(s + "\n");
                }
                i++;
            }
        } catch(IOException ie) {
            ie.printStackTrace();
        }
    }

    public static void main(String[] args) {
        MagXML magXML = new MagXML(new File("/export/home/baga/Documents/Book/TA/data IR/doc.text"));
        magXML.explores();
    }
}
